#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<netinet/in.h>
#include<sys/socket.h>
#include<unistd.h>
#include<memory>
#include<string.h>

int main(void)
{
    //创建socket对象
    int sock=socket(AF_INET,SOCK_DGRAM,0);
    if(sock<0)
    {
        printf("socket() failed\n");
        return 1;
    }
    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0)
    {
        printf("socket() failed!\n");
        return 1;
    }
    //准备服务器的地址
    struct sockaddr_in serverAddr;
    memset(&serverAddr,0,sizeof(serverAddr));
    serverAddr.sin_family=AF_INET;
    serverAddr.sin_port=htons(8000);//端口号
    serverAddr.sin_addr.s_addr=htonl(INADDR_ANY);//主机号要转成网络号，长整型
    //准备服务端地址
    struct sockaddr_in serverAddr;
    memset(&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(8000); //绑定8000端口
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); //任意IP地址

    //绑定socket对象到服务地址
    if(bind(sock,(struct sockaddr*)&serverAddr,sizeof(serverAddr)) < 0)
    {
        printf("bind() failed!\n");
        close(sock);
        return 1;
    }
    //绑定socket对象到服务端地址
    if (bind(sock, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) < 0)
    {
        printf("bind() failed!\n");
        close(sock);
        return 1;
    }

    //接受并处理客户端发送的数据
    char recvData[1024];
    struct sockaddr_in clientAddr;
    socklen_t len=sizeof(clientAddr);
    while(true)
    {
        int retRecv=recvfrom(sock,recvData,sizeof(recvData),0,(struct sockaddr*)&clientAddr,&len);
        if(retRecv < 0)
        {
            printf("recvfrom() failed!\n");
            continue;
        }
        printf("Received data from client(%s:%d):%s\n",inet_ntoa(clientAddr.sin_addr),ntohs(clientAddr.sin_port),recvData);

        char sendData[1024]="Hello Client I am Server";
        int retSend=sendto(sock,sendData,strlen(sendData),0,(struct sockaddr*)&clientAddr,sizeof(clientAddr));
        if(retSend < 0)
        {
            printf("sendto() failed!\n");
            continue;
        }
    }
    /*
    //这里的sock返回的是一个文件描述符
    close(sock);
    return 0;
    */
    //接收并处理客户端发送的数据
    char recvData[1024];
    struct sockaddr_in clientAddr;
    socklen_t len = sizeof(clientAddr);
    while (1)
    {
        int retRecv = recvfrom(sock, recvData, sizeof(recvData), 0, (struct sockaddr*)&clientAddr, &len);
        if (retRecv < 0)
        {
            printf("recvfrom() failed!\n");
            continue;
        }

        //输出客户端发来的数据,输出客户端的IP地址端口号以及发送的信息
        printf("Received data from client (%s:%d): %s\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port), recvData);

        //回复客户端
        char sendData[] = "Hello, client! I am server!";
        int retSend = sendto(sock, sendData, strlen(sendData), 0, (struct sockaddr*)&clientAddr, sizeof(clientAddr));
        if (retSend < 0)
        {
            printf("sendto() failed!\n");
            continue;
        }
    }

    //关闭socket对象
    close(sock);

    return 0;
}
// int main(void)
// {
//     int sock=socket(AF_INET,SOCK_DGRAM,0);
//     if(sock<0)
//     {
//         printf("socket() failed\n");
//         return 1;
//     }
//     struct sockaddr_in serverAddr;
//     memset(&serverAddr,0,sizeof(serverAddr));
//     serverAddr.sin_family=AF_INET;
//     //绑定8000端口号
//     serverAddr.sin_port=htons(8000);
//     //任意IP地址
//     serverAddr.sin_addr.s_addr=htonl(INTADDR_ANY);
//     //接受并处理客户端发送的数据
//     char recvData[1024];
//     struct sockaddr_in clientAddr;
//     socklen_t len=sizeof(clientAddr);
//     while(true)
//     {
//         int retRecv=recvfrom(sock,recvData,sizeof(recvData),0,)
//     }
//     return 0;
// }